package com.example.wenmini.controller;


import com.example.wenmini.config.AjaxResult;
import com.example.wenmini.entity.Questionnaire;
import com.example.wenmini.entity.QuestionnaireFillRequest;
import com.example.wenmini.service.FillService;
import com.example.wenmini.service.ParseService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/fill")
@Slf4j
public class FillController {

    @Autowired
    private FillService fillService;

    @PostMapping
    public Object fillQuestionnaire(@RequestBody QuestionnaireFillRequest request) {
        log.info("收到问卷填写请求，URL：{}", request.getUrl());
        try {
            // 验证请求参数
            String url = request.getUrl();
            if (url == null || url.trim().isEmpty()) {
                return ResponseEntity.badRequest().body("问卷URL不能为空");
            }
            Integer waitTime = request.getWaitTime();
            if (waitTime == null || waitTime < 1) {
                return ResponseEntity.badRequest().body("填写间隔必须大于0秒");
            }
            Integer count = request.getCount();
            if (count == null || count < 1) {
                return ResponseEntity.badRequest().body("填写次数必须大于0");
            }
            List<?> questionConfigs = request.getQuestionConfigs();
            if (questionConfigs == null || questionConfigs.isEmpty()) {
                return ResponseEntity.badRequest().body("问题配置不能为空");
            }
            // 启动问卷填写任务
            fillService.createFillTask(request);
            return AjaxResult.success("任务创建成功");
        } catch (Exception e) {
            log.error("创建问卷填写任务失败：", e);
            return ResponseEntity.internalServerError().body("创建问卷填写任务失败：" + e.getMessage());
        }
    }



}
